//
//  XZHSearchViewController.m
//  Piece
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHSearchViewController.h"
#import "XZHSearchBar.h"
#import "TYTabButtonPagerController.h"
#import "DXYHomeViewController.h"
#import "XZHSearchIllustrationViewController.h"


@interface XZHSearchViewController ()<UISearchBarDelegate,TYPagerControllerDataSource>
@property (nonatomic, strong) TYTabButtonPagerController *pagerController;
/** 取消按钮 */
@property (nonatomic, strong) UIButton *cancleButton;
/** 搜索框 */
@property (nonatomic, strong) XZHSearchBar *searchBar;
@end

@implementation XZHSearchViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.99 blue:1.00 alpha:1.00];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    searchView.backgroundColor = [UIColor colorWithRed:0.94 green:0.95 blue:0.97 alpha:1.00];
    [self.view addSubview:searchView];
    [searchView setTag:1220];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.86 green:0.87 blue:0.88 alpha:1.00];
    [self setUpSearchBar];
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];
    backgroundView.backgroundColor = [UIColor colorWithRed:0.98 green:0.99 blue:1.00 alpha:1.00];
    [self.view addSubview:backgroundView];
    
    UIImageView *placeholderImageView = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 165) / 2, 120, 165, 110)];
    placeholderImageView.image = [UIImage imageNamed:@"search_all"];
    [self.view addSubview:placeholderImageView];
    [placeholderImageView setTag:1221];
}

#pragma mark 创建搜索框
- (void)setUpSearchBar {
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(10, 25, ScreenWidth , 40)];
    self.searchBar = [[XZHSearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 60, 28)];
    self.searchBar.placeholder = @"";
    self.searchBar.delegate = self;
    self.searchBar.layer.cornerRadius = 6;
    self.searchBar.layer.masksToBounds = YES;
    [titleView addSubview:self.searchBar];
    
    self.cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancleButton.frame = CGRectMake(self.searchBar.frame.origin.x + self.searchBar.frame.size.width + 10, 2, 35, 25);
    [self.cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancleButton setTitleColor:[UIColor colorWithRed:0.60 green:0.60 blue:0.60 alpha:1.00] forState:UIControlStateNormal];
    [self.cancleButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    self.cancleButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.cancleButton addTarget:self action:@selector(cancelBtnTouch) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:self.cancleButton];
    
    [[self.view viewWithTag:1220] addSubview:titleView];
}

- (void) cancelBtnTouch {
    self.searchBar.text = nil;
    [self.searchBar resignFirstResponder];
    [self .navigationController popViewControllerAnimated:YES];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [[self.view viewWithTag:1221] removeFromSuperview];
    [self addPagerController];
}

- (void)addPagerController
{
    TYTabButtonPagerController *pagerController = [[TYTabButtonPagerController alloc]init];
    pagerController.dataSource = self;
    pagerController.adjustStatusBarHeight = YES;
    pagerController.barStyle = _variable ? TYPagerBarStyleProgressBounceView: TYPagerBarStyleProgressView;
    pagerController.normalTextColor =[UIColor colorWithRed:0.76 green:0.79 blue:0.83 alpha:1.00];
    pagerController.selectedTextColor = [UIColor colorWithRed:0.76 green:0.79 blue:0.83 alpha:1.00];
    pagerController.normalTextFont = [UIFont systemFontOfSize:12];
    pagerController.selectedTextFont = [UIFont systemFontOfSize:12.5];
    pagerController.cellWidth = ScreenWidth / 4 - 2;
    pagerController.progressColor = [UIColor colorWithRed:0.59 green:0.72 blue:0.86 alpha:1.00];
    pagerController.progressWidth = 25;
    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    _pagerController = pagerController;
}

- (void)scrollToRamdomIndex
{
    [_pagerController moveToControllerAtIndex:arc4random() % 4 animated:NO];
}

- (NSInteger)numberOfControllersInPagerController
{
    return 4;
}


- (NSString *)pagerController:(TYPagerController *)pagerController titleForIndex:(NSInteger)index
{
    if (index == 0) { return @"插图";}
    else if (index == 1) {return @"阅读";}
    else if (index == 2) {return @"音乐";}
    else {return @"影视";}
//    else {return @"作者/音乐人";};
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index
{
    
    XZHSearchIllustrationViewController *illustrationVC = [[XZHSearchIllustrationViewController alloc] init];
    illustrationVC.key = self.searchBar.text;
    illustrationVC.curIndex = index;
    return illustrationVC;
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _pagerController.view.frame = CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64);
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
